/**
*	拖动
*
*/

(function($){

    var dragobj={};
	var oldObj = null;
	$.move=function(obj1,obj2){

		$(obj1).animate({left : $(obj2).offset().left,
			top : $(obj2).offset().top,
			width : $(obj2).width(),
			height : $(obj2).height()
			},100);
	}

	$(document).mousemove(function(e){
		  e=e||event;
		  var mousePos=$.mousePosition(e);
        if(dragobj.obj!=null){
			$(dragobj.obj).css({
				left: mousePos.x-dragobj.relative.x,
				top: mousePos.y-dragobj.relative.y
			});
        }
	});
	$(document).bind("selectstart",function(){return false});
	$(document).focus(function(){$(document).mouseup()});
	$(document).blur(function(){$(document).mouseup()});
	$(document).mouseup(function(e){
		if(dragobj.obj!=null){
				e=e||event;
				var mousePos=$.mousePosition(e);
					var targetElem=$.getMouseUpDiv(e);
					$(dragobj.obj).removeAttr("style");
					if($(targetElem).attr("class")!="line"){
						$(dragobj.otemp).replaceWith(dragobj.obj);
					}else{
						$(targetElem).replaceWith(dragobj.obj);
						$(dragobj.otemp).replaceWith(targetElem);
					}
				oldObj = dragobj.obj;
				dragobj={};
				$(".line").easydrag();
        }
	});
	$(document).dblclick(function(e){
		e=e||event;
		var targetElem = e.srcElement ? e.srcElement : e.target;
		if($(targetElem.parentNode).attr("class")=="line" || $(targetElem.parentNode).attr("class")=="textSelect"){
			alert();
		}
	});
	$.inner=function (_obj,e){
		var mousePos=$.mousePosition(e);
        var a=$.getElemXY(_obj);
        if(mousePos.x>=a.elemLeft  && mousePos.x<=(a.elemLeft+a.elemWidth)&&mousePos.y>=a.elemTop&&mousePos.y<=(a.elemTop+a.elemHeight)){
                if(mousePos.y<=(a.elemTop+a.elemHeight/2))
						// 在上方
                        return 1;
                else
						//在下方
                        return 2;
        }else
                return 0;
}
	$.getRowId=function(e){
		var mousePos=$.mousePosition(e);
		var row=$(".row");
		for(var i=0;i<row.length;i++){
			 var a=$.getElemXY(row.get(i));
			if(mousePos.x>=a.elemLeft && mousePos.x<=a.elemLeft+a.elemWidth)
				return row.eq(i).attr("id");
		}
	}

	$.getMouseUpDiv=function(e){
		var mousePos=$.mousePosition(e);
		var rowId=$.getRowId(e);
		var divChildren=$("#"+rowId).children(".line");
		for(var i=0;i<divChildren.length;i++){
			var a=$.getElemXY(divChildren.get(i));
			if(dragobj.obj==divChildren.get(i))
				continue;
			if(mousePos.y>=a.elemTop && mousePos.y<=a.elemTop+a.elemHeight)
				return divChildren.get(i);
		}
	}
	$.createtmpl=function(e){
		var mousePos=$.mousePosition(e);
		var rowId;
		var row=$(".row");
		for(var i=0;i<row.length;i++){
			 var a=$.getElemXY(row.get(i));
			if(mousePos.y>=a.elemTop && mousePos.y<=a.elemTop+a.elemHeight)
				rowId=row.eq(i).attr("id");
		}
		var blockDiv=$("#"+rowId+".line");
		var blockDivNum=blockDiv.length;
		var blockDivChildren;
		var blockDivChildrenNum;
		for(var i=0;i<blockDivNum;i++){
			var a=$.getElemXY(blockDiv.get(i));
			if(mousePos.x>=a.elemLeft && mousePos.x<=a.elemLeft+a.elemWidth){
				blockDivChildren=blockDiv.eq(i).children(".mo");
				blockDivChildrenNum=blockDivChildren.length;
				if(blockDivChildrenNum<=0 ||(blockDivChildrenNum==1 && blockDivChildren.get(0)==dragobj.obj)){
					blockDiv.eq(i).append(dragobj.otemp);
					$(dragobj.otemp).css({
						width:"auto"
					});
				}
			}
		}
		 for(var i=0;i<blockDivChildrenNum;i++){
				var obj=blockDivChildren.get(i);
				if(obj==dragobj.obj)
                        continue;
				// 鼠标的整体位置
                var b=$.inner(obj,e);
                if(b==0)
                        continue;
                $(dragobj.otemp).width($(obj).width());
                if(b==1){
                         $(obj).before(dragobj.otemp);
                }else{
						$(obj).after(dragobj.otemp);
                }
				return;
        }
	}

	$.getElemXY=function(_elem){
        var elemTop=$(_elem).offset().top;
        var elemLeft=$(_elem).offset().left;
        var elemWidth=$(_elem).width();;
        var elemHeight=$(_elem).height();
		return {'elemTop':elemTop,'elemLeft':elemLeft,'elemWidth':elemWidth,'elemHeight':elemHeight};
	};

	//获取鼠标相对于文档的位置
	$.mousePosition=function(e){
		if(e.pageX || e.pageY){
			return {x:e.pageX, y:e.pageY};
		}
		return {
			x:e.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:e.clientY + document.body.scrollTop  - document.body.clientTop
		};
	}
	// set an element as draggable - allowBubbling enables/disables event bubbling
	$.fn.easydrag = function(){
		return this.each(function(){
			// when an element receives a mouse press
			$(this).mousedown(function(e){
					   if(dragobj.obj!=null)
                                return false;
                        e=e||event;
						var mousePos=$.mousePosition(e);

						// 当前div获取焦点
						var thisId = $(this).attr("id");
						if($("#"+thisId+"_text").attr("class")!=null){
							$("#"+thisId+"_text").addClass("textSelect").css({
								overflow:'',
								wordBreak:'break-all'
							});
						}
						// div失去焦点
						if(oldObj!=this){
							var oldObjId = $(oldObj).attr("id");
							$("#"+oldObjId+"_text").removeAttr("class").css({
								overflow:'hidden',
								wordBreak:''
							});
						}

						dragobj.obj=this;
                        dragobj.position=$.getElemXY(this);
                        dragobj.relative={'x':mousePos.x-dragobj.position.elemLeft,'y':mousePos.y-dragobj.position.elemTop};
						/*
                       dragobj.otemp=document.createElement("div");
						$(dragobj.otemp).css({
							width: dragobj.position.elemWidth,
							height: dragobj.position.elemHeight
						});

						$(dragobj.otemp).addClass("line");
						*/
						// 设置成半透明状态
						$(dragobj.obj).css({
							opacity:0.5
						}).select();

						// 克隆拖动对象
						dragobj.otemp=$(dragobj.obj).clone().get(0);
						// 设置拖动对象的样式
						$(dragobj.obj).css({
							width: $(dragobj.obj).width(),
							height:  $(dragobj.obj).height(),
							left: mousePos.x-dragobj.relative.x,
							top: mousePos.y-dragobj.relative.y,
							zIndex: 1100,
							position: "absolute"
						}).before(dragobj.otemp);// 当前拖动对象之前插入临时对象
                        return false;
			});
		});
	};

})(jQuery);